package cn.circle.system.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import cn.circle.commom.dao.impl.BaseDaoImpl;
import cn.circle.system.dao.ISystemDao;
import com.graduation.system.model.SysCustomer;

@Repository
public class SystemDao extends BaseDaoImpl<SysCustomer> implements ISystemDao{

	@SuppressWarnings("unchecked")
	public List<SysCustomer> login(String userName,String userPwd) {
		String hql = " from SysCustomer s where s.userName = '"+userName+"' and s.passWord = '"+userPwd+"'";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return  query.list();
	}

	
	
	public boolean register(SysCustomer c) {
//		String hql = "update SysCustomer(USER_NAME,PASSWORD) values('"+c.getUserName()+"','"+c.getPassWord()+"') ";
//		StringBuffer hql = new StringBuffer(
//				"update SysCustomer s set ");
//		
//		//拼接修改条件
//		if(c.getUserName() != null && !"".equals(c.getUserName())){
//			hql.append("  s.userName = '"+c.getUserName()+"' ");
//		}
////		if(c.getLoginName() != null && !"".equals(c.getLoginName())){
////			hql.append(" , s.loginName = '"+c.getLoginName()+"' ");
////		}
////
//		if(c.getPassWord() != null && !"".equals(c.getPassWord())){
//			hql.append(" , s.passWord = '"+c.getPassWord()+"' ");
//		}
////		if(c.getSysDepartment().getDepId() != null && !"".equals(c.getSysDepartment().getDepId())){
////			hql.append(" , s.sysDepartment.depId = "+c.getSysDepartment().getDepId()+" ");
////		}
////		if(c.getSex() != null && !"".equals(c.getSex())){
////			hql.append(" , s.sex = "+c.getSex()+" ");
////		}
////		if(c.getEmail() != null ){
////			hql.append(" , s.email = '"+c.getEmail()+"' ");
////		}
////		if(c.getCustType() != null && !"".equals(c.getCustType())){
////			hql.append(" , s.custType = "+c.getCustType()+" ");
////		}
////		if(c.getPhone() != null ){
////			hql.append(" , s.phone = '"+c.getPhone()+"' ");
////		}
////		if(c.getStatus() != null && !"".equals(c.getStatus())){
////			hql.append(" , s.status = "+c.getStatus()+" ");
////		}
//
//		hql.append(" where s.custId = null");
//		Query query = super.sessionFactory.getCurrentSession().createQuery(hql.toString());
//		return (query.executeUpdate() > 0);
//		
//		SessionFactory sf = new Configuration().configure().buildSessionFactory();
//		   Session session = sf.openSession();
//		   Transaction tx = session.beginTransaction();
//		      
//		    student st=new student();
//		    st.setId("0001");
//		    st.setUserName("Wang");
//		    st.setpassWord("123");
//		    session.save(st);
//		    tx.commit();
//		    session.close();

		String hql = " from SysCustomer s where s.userName = '"+c.getUserName()+"'";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		List<SysCustomer> cml = query.list();
		if(cml.isEmpty()){
			String sql = "insert into SYS_CUSTOMER(USER_NAME,PASSWORD,DEP_ID) values('"+c.getUserName()+"','"+c.getPassWord()+"','"+c.getSysDepartment()+"')";
			Query query1 = super.sessionFactory.getCurrentSession().createSQLQuery(sql);
			return (query1.executeUpdate() > 0);
		}else{
			return false;
		}
		
		
	}
}
